package com.epfresh.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.activity.OrderMinusDetailActivity;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.universalimageloader.core.imageaware.ImageViewAware;
import com.epfresh.bean.MinusRecordBean;
import com.epfresh.global.BaseApplication;
import com.epfresh.utils.FormatUtil;

import java.util.List;

/**
 * Created by zzq on 2016/7/19.
 */
public class MinusRecordExpandListAdapter extends BaseExpandableListAdapter implements View.OnClickListener {

    public interface OnCheckChangeListener {
        public void onCheckChange(boolean isCheck);
    }
    private OnCheckChangeListener onCheckChangeListener;
    public ExpandableListView lv;

    public Context context;
    public List<MinusRecordBean.ContentBean> groupList;
    private AdapterView.OnItemClickListener onItemClickListener;

    public int type=1;

    private OnBottomClickListener onBottomClickListener;
    private OnNotifyDataSetChangedListener onNotifyDataSetChangedListener;


    public MinusRecordExpandListAdapter(Context context, ExpandableListView lv, List<MinusRecordBean.ContentBean> groupList, int type) {
        this.context = context;
        this.groupList = groupList;
        this.lv = lv;
        this.type = type;
    }

    public OnCheckChangeListener getOnCheckChangeListener() {
        return onCheckChangeListener;
    }

    public void setOnCheckChangeListener(OnCheckChangeListener onCheckChangeListener) {
        this.onCheckChangeListener = onCheckChangeListener;
    }

    @Override
    public void notifyDataSetChanged() {
        if (onNotifyDataSetChangedListener != null) {
            onNotifyDataSetChangedListener.OnNotifyDataSetChangedListener(groupList.size());
        }
        expendList();
        super.notifyDataSetChanged();
    }


    public OnNotifyDataSetChangedListener getOnNotifyDataSetChangedListener() {
        return onNotifyDataSetChangedListener;
    }

    public void setOnNotifyDataSetChangedListener(OnNotifyDataSetChangedListener onNotifyDataSetChangedListener) {
        this.onNotifyDataSetChangedListener = onNotifyDataSetChangedListener;
    }

    public OnBottomClickListener getOnBottomClickListener() {
        return onBottomClickListener;
    }

    public void setOnBottomClickListener(OnBottomClickListener onBottomClickListener) {
        this.onBottomClickListener = onBottomClickListener;
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {

        List<MinusRecordBean.ContentBean.ListBean> configs = groupList.get(groupPosition).getList();
        return configs == null ? 0 : configs.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {

        return groupList.get(groupPosition).getList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    /**
     * 组和子元素是否持有稳定的ID,也就是底层数据的改变不会影响到它们。
     *
     * @return
     * @see android.widget.ExpandableListAdapter#hasStableIds()
     */
    @Override
    public boolean hasStableIds() {
        return true;
    }

    /**
     * 获取显示指定组的视图对象
     *
     * @param groupPosition 组位置
     * @param isExpanded    该组是展开状态还是伸缩状态
     * @param convertView   重用已有的视图对象
     * @param parent        返回的视图对象始终依附于的视图组
     * @return
     * @see android.widget.ExpandableListAdapter#getGroupView(int, boolean, View,
     * ViewGroup)
     */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_record_minus_group, null, false);
            vh.tv_store_name = (TextView) convertView.findViewById(R.id.tv_store_name);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        MinusRecordBean.ContentBean orders = groupList.get(groupPosition);
        vh.tv_store_name.setText(orders.getStoreName());
        convertView.setTag(vh);
        convertView.setClickable(true);
        return convertView;
    }


    /**
     * 获取一个视图对象，显示指定组中的指定子元素数据。
     *
     * @param groupPosition 组位置
     * @param childPosition 子元素位置
     * @param isLastChild   子元素是否处于组中的最后一个
     * @param convertView   重用已有的视图(View)对象
     * @param parent        返回的视图(View)对象始终依附于的视图组
     * @return
     * @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean, View,
     * ViewGroup)
     */
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        convertView = getChildNormalView(groupPosition, childPosition, isLastChild, convertView, parent);
        return convertView;
    }

    public View getChildNormalView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder vh = null;
        if (convertView == null) {
            vh = new ChildViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_record_minus_child, null, false);
            vh.order_no = (TextView) convertView.findViewById(R.id.order_no);
            vh.iv_logo = (ImageView) convertView.findViewById(R.id.iv_logo);
            vh.iv_promotion = (ImageView) convertView.findViewById(R.id.iv_promotion);
            vh.tv_type = (TextView) convertView.findViewById(R.id.tv_type);
            vh.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            vh.tv_activity_promotion =  convertView.findViewById(R.id.tv_activity_promotion);
            vh.tv_activity_second = convertView.findViewById(R.id.tv_activity_second);
            vh.tv_order_type = (TextView) convertView.findViewById(R.id.tv_order_type);
            vh.tv_number = (TextView) convertView.findViewById(R.id.tv_number);
            vh.tv_total = (TextView) convertView.findViewById(R.id.tv_total);
            vh.tv_minus_money = (TextView) convertView.findViewById(R.id.tv_minus_money);
            vh.tv_minus_number = (TextView) convertView.findViewById(R.id.tv_minus_number);
            vh.ll_order_child = convertView.findViewById(R.id.ll_order_child);
            convertView.setTag(vh);
        } else {
            vh = (ChildViewHolder) convertView.getTag();
        }
        MinusRecordBean.ContentBean logisticsOrder = groupList.get(groupPosition);
        List<MinusRecordBean.ContentBean.ListBean> orders = logisticsOrder.getList();
        vh.order_no.setText("减免退编号：" + orders.get(childPosition).getJmtNo());
        ImageLoader.getInstance().displayImage(orders.get(childPosition).getProductImgUrl(), vh.iv_logo);
        String promotionMarkUrl = orders.get(childPosition).getMarkUrl();
        ImageLoader.getInstance().displayImage(promotionMarkUrl, new ImageViewAware(vh.iv_promotion), BaseApplication.getTagOptions(promotionMarkUrl) , null, null, null);

        vh.tv_title.setText(orders.get(childPosition).getProductTitle());
/*        vh.tv_order_type.setText(orders.get(childPosition).getPriceDescription());
        vh.tv_number.setText("X" + orders.get(childPosition).getProductCount());
        vh.tv_total.setText(orders.get(childPosition).getOrderSumPrice() + "元");
        vh.tv_minus_money.setText(orders.get(childPosition).getJmtTotalPrice());
        vh.tv_minus_number.setText(orders.get(childPosition).getProductCount());*/

        vh.tv_order_type.setText(orders.get(childPosition).getJmtRefundStatusTitle());
        vh.tv_number.setText(orders.get(childPosition).getProductPriceUnit()+"   "+"X" + FormatUtil.subZeroAndDot(orders.get(childPosition).getProductCount() + ""));
        vh.tv_total.setText(orders.get(childPosition).getProductTotalPrice() + "元");
        vh.tv_minus_money.setText("减免退金额："+orders.get(childPosition).getJmtTotalPrice()+"元");
        if(orders.get(childPosition).getJmtProductCount()<=0){
            vh.tv_minus_number.setVisibility(View.GONE);
        }else{
            vh.tv_minus_number.setText("退货："+orders.get(childPosition).getJmtProductCount()+orders.get(childPosition).getProductChargeUnit());
        }
        if (2==(orders.get(childPosition).getSalesType())) {//预售
            vh.tv_type.setVisibility(View.VISIBLE);
            String html="<font color=\"#00ffffff\">a预售】</font>"+orders.get(childPosition).getProductTitle();
            vh.tv_title.setText(Html.fromHtml(html));
        } else {//现售
            vh.tv_type.setVisibility(View.GONE);
            vh.tv_title.setText(orders.get(childPosition).getProductTitle());
        }
        String promotionType = orders.get(childPosition).getPromotionType();
        if ("1".equals(promotionType)) {//1促销2秒杀
            vh.tv_activity_promotion.setVisibility(View.VISIBLE);
            vh.tv_activity_second.setVisibility(View.GONE);
        }else  if ("2".equals(promotionType)) {//1促销2秒杀
            vh.tv_activity_promotion.setVisibility(View.GONE);
            vh.tv_activity_second.setVisibility(View.VISIBLE);
        }else {
            vh.tv_activity_promotion.setVisibility(View.GONE);
            vh.tv_activity_second.setVisibility(View.GONE);
        }

        vh.ll_order_child.setTag(R.id.item_key_position, orders.get(childPosition).getId());
        vh.ll_order_child.setOnClickListener(this);
        return convertView;
    }

    @Override
    public void onClick(View v) {
        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof String) {
            String id = (String) tag;
// 交货码，查看物流，取消订单，确认收货，支付， 详情
            switch (v.getId()) {
                case R.id.ll_order_child:
                    Intent detail = new Intent(context, OrderMinusDetailActivity.class);
                    detail.putExtra("id", id);
                    context.startActivity(detail);
                    break;
            }
        }
    }

    /**
     * 是否选中指定位置上的子元素。
     *
     * @param groupPosition
     * @param childPosition
     * @return
     * @see android.widget.ExpandableListAdapter#isChildSelectable(int, int)
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }


    protected void expendList() {

        if (lv != null)
            for (int i = 0; i < groupList.size(); i++) {
                lv.expandGroup(i);
            }
    }



    public class ViewHolder {
        TextView tv_store_name;
    }

    public class ChildViewHolder {
        TextView order_no;
        ImageView iv_logo;
        ImageView iv_promotion;
        TextView tv_type;
        TextView tv_title;
        TextView tv_activity_promotion,tv_activity_second;
        TextView tv_order_type;
        TextView tv_number;
        TextView tv_total,tv_minus_number,tv_minus_money;

        View ll_order_child;

    }


    public interface OnBottomClickListener {

        public void onBottomClickListener(int position, int type);
    }

    public interface OnNotifyDataSetChangedListener {

        public void OnNotifyDataSetChangedListener(int size);
    }

}
